using CaipuGuanliXitong.Models;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();

// 注册数据库上下文
builder.Services.AddSingleton<CookBookDbContext>();

var app = builder.Build();

// 检查是否需要重新创建表
var recreateTables = Environment.GetEnvironmentVariable("RECREATE_DATABASE_TABLES");
if (!string.IsNullOrEmpty(recreateTables) && recreateTables.ToLower() == "true")
{
    Console.WriteLine("开始重新创建数据库表...");
    if (app.Services.GetService(typeof(CookBookDbContext)) is CookBookDbContext context)
    {
        try
        {
            context.ReCreateDatabase();
            Console.WriteLine("数据库表已成功重新创建！");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"重新创建数据库表时出错: {ex.Message}");
        }
    }
}

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseRouting();

app.UseAuthorization();

app.MapStaticAssets();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}")
    .WithStaticAssets();

app.Run();